<?php
class Twitter
{
  public static function post($share)
  {
    $consumer_key = 'SgPEBc6a3k02xfjmAveDZQ';
    $consumer_secret = '3zTsAxaU1RFHiSFZFHSlv0HIsnRpc0U5wj9XZyMI5w';
    $oauth_token = '234267121-GpILP0bAQwUY5PGAT2zKNpYTYXYvBg6zvS9rWCRl';
    $oauth_token_secret = 'hjPul46PFaLeUpMhXoGLQM3hoFEbtGCPpvl7Y7K8';
    
    // The twitter API address
    $url = 'http://api.twitter.com/1/statuses/update.json';

    $ContentToTweet = '';
    
    if($offer = $share->getOffer())
    {
      if($offer->getDiscountPercent() >= 1)
      {
        $ContentToTweet .= $offer->getDiscountPercent().'% in ';
      }
      $c = new Criteria();
      $c->add(OfferCityPeer::OFFER_ID, $offer->getId());
      $c->addJoin(CityPeer::ID, OfferCityPeer::CITY_ID);
      $c->addAscendingOrderByColumn(CityPeer::NAME);
      $cities = CityPeer::doSelect($c);
      
      $city = $cities[rand(0, count($cities) - 1)];

      $ContentToTweet .= $city->getName().': ';
      
      $offer_url = ' '.sfContext::getInstance()->getController()->genUrl($offer->getRouteName().'?id='.$offer->getId(), true);
      
      $ContentToTweet .= Utils::limitString($share->getTitle(), (137 - (strlen($ContentToTweet) + strlen($offer_url))), true);
      $ContentToTweet .= $offer_url;
    }
    
    if($ContentToTweet && strlen($ContentToTweet) <= 140)
    {
      $connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
      $content = $connection->post($url, array('status' => $ContentToTweet));

      return $content;
    }
    
    return null;
  }
}
